/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Sidang;
import Entity.TA;
import Entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import library.Koneksi;

/**
 *
 * @author mr2
 */
public class TAModel {
    
    Connection koneksi;
    User user;
    Sidang sidang;
    TA ta;
    
    public TAModel(){
        koneksi = Koneksi.getKoneksi();
    }
    
    //Method untuk mendapatkan id TA mahasiswa yang telah mendaftar.
    public int getIdTA(User user){
        PreparedStatement prepare = null;
        ResultSet result = null;
        
        int idTA = 0;
        
        try {   
            String sql = "select id_ta from ta where status_ta = '1' and id_user = ? ";                      
            prepare = koneksi.prepareStatement(sql);
            prepare.setString(1, user.getIdUser());
            result = prepare.executeQuery();
            
            if(result.next()){                                                     
               idTA = result.getInt("id_ta");
            }else{
               idTA = 0;
            }
                    
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            return 0;
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
        return idTA;
    } 
    
    //Method untuk melakukan input data TA oleh mahasiswa ketika pertama kali mengisi data TA
    public void inputDataTA(TA ta){
        
        PreparedStatement prepare = null;
        
        try{
            String sql = "INSERT into ta(id_user, judul, pembimbing_1, pembimbing_2, pembimbing_luar, nama_lab) values(?,?,?,?,?,?)";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setString(1, ta.getUser().getIdUser());
            prepare.setString(2, ta.getJudul());
            prepare.setString(3, ta.getPembimbing1());
            prepare.setString(4, ta.getPembimbing2());
            prepare.setString(5, ta.getPembimbingLuar());
            prepare.setString(6, ta.getNamaLab());
            
            prepare.executeUpdate();
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }      
    }
    
    //Method untuk menampilkan detail dari data TA dari mahasiswa
    public TA getDetailDataTA (TA ta){
        
        PreparedStatement prepare = null;
        ResultSet result = null;
        User user = new User();
        
        try{
            
            String sql = "Select ta.id_user, nama_user, id_ta, judul, pembimbing_1, pembimbing_2, pembimbing_luar, nama_lab, status_ta "
                         + "from ta, user where user.id_user = ta.id_user and ta.id_ta = ? and ta.status_ta = '1' ";
            prepare = koneksi.prepareStatement(sql);

            prepare.setInt(1, ta.getIdTA());
            
            result = prepare.executeQuery();
            
            if(result.next()){
                user.setIdUser(result.getString("id_user"));
                user.setNamaUser(result.getString("nama_user"));
                ta.setUser(user);
                ta.setIdTA(result.getInt("id_ta"));
                ta.setJudul(result.getString("judul"));
                ta.setPembimbing1(result.getString("pembimbing_1"));
                ta.setPembimbing2(result.getString("pembimbing_2"));
                ta.setPembimbingLuar(result.getString("pembimbing_luar"));
                ta.setNamaLab(result.getString("nama_lab"));
                ta.setStatusTA(result.getString("status_ta"));
            }
            return ta;
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
        }
        return ta;
    }
    
    //Method untuk melakukan perubahan terhadap data TA, perubahan ini dilakukan oleh koordinator TA
    public boolean updateDataTA(TA ta){
        
        PreparedStatement prepare = null;
        boolean hasil = true;
        String statusTA = ta.getStatusTA();
        String sql = null;
        
        try{
                sql = "update ta set judul = ?, pembimbing_1 = ?, pembimbing_2 = ?, pembimbing_luar = ?, nama_lab = ? where id_ta = ? ";
            
                prepare = koneksi.prepareStatement(sql);

                prepare.setString(1, ta.getJudul());
                prepare.setString(2, ta.getPembimbing1());
                prepare.setString(3, ta.getPembimbing2());
                prepare.setString(4, ta.getPembimbingLuar());
                prepare.setString(5, ta.getNamaLab());
                prepare.setInt(6, ta.getIdTA());
                int status = prepare.executeUpdate();
                if(status > 0){
                hasil = true;
                }else{
                    hasil = false;
                }
            
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
        
        return hasil;
    }
    
    /*melakukan perubahan status ta, digunakan oleh koordinator TA pada fitur untuk melakukan menon aktifkan pendaftar TA,
    karena sidang TA dibatalkan*/
    public void updateStatusTA(TA ta){
        
        PreparedStatement prepare = null;
        String statusTA = ta.getStatusTA();
        String sql = null;
        
        try{
                sql = "update ta set status_ta = ? where id_ta = ? ";
            
                prepare = koneksi.prepareStatement(sql);
                
                prepare.setString(1, ta.getStatusTA());
                prepare.setInt(2, ta.getIdTA());
                prepare.executeUpdate();
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
    
    public List<TA> ListAktifTA(TA ta){
        
        User user = new User();
        PreparedStatement prepare = null;
        ResultSet result = null;
        List<TA> list = new ArrayList<TA>();
        
        try{
            String sql = "select * from TA";
            
            prepare = koneksi.prepareStatement(sql);
            
            result = prepare.executeQuery();
            
            while(result.next()){
                
                //sidangta.setId_user(result.getString("id_user"));
                user.setIdUser(result.getString("id_user"));
                ta.setUser(user);
                ta.setNamaLab(result.getString("nama_lab"));
                ta.setJudul(result.getString("judul"));
                ta.setIdTA(result.getInt("id_ta"));
                      
                list.add(ta);
            }
            return list;
            
        }catch(SQLException ex){
            return list;
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
}   
   